package com.app.base.utlis.email;

import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Date;

@Component
@Slf4j
public class EmailUtils {

    @Resource
    private JavaMailSender mailSender;

    public static SimpleMailMessage simpleSend(Mail mail){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setSubject(mail.getSubject());
        message.setFrom(mail.getFrom());
        message.setTo(mail.getTo());
        message.setSentDate(new Date());
        message.setText(mail.getText());
        return message;
    }

    public void htmlEmail(Mail mail) {
        try{
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper msgHelper = new MimeMessageHelper(message, true);
            msgHelper.setFrom(mail.getFrom());
            msgHelper.setTo(mail.getTo());
            msgHelper.setSubject(mail.getSubject());
            msgHelper.setText(mail.getText(), true);
            mailSender.send(message);
        }catch(Exception e){
            log.error(e.getMessage());
        }


    }



}
